function getDateStr(dateObj) {
    return `${dateObj.getFullYear()}/${dateObj.getMonth() + 1}/${dateObj.getDate()}`;
}
function getDateHref(dateStr) {
    return dateStr.replace(/\//g, '.');
}
function convertDateStr(dateStr) {
    return dateStr.replace(/\./g, '/');
}
export default function (planId) {
    let currentChoose = null;
    let yearList = [];
    let monthList = [];
    let weekList = [];
    let dayList = [];
    if ((yearList && monthList && weekList && dayList)) {
        var years = [2024, 2025, 2026, 2027];
        const yearReg = /^year_(.+)/;
        const monthReg = /^month_(.+)/;
        const weekReg = /^week_(.+)/;
        const dayReg = /^day_(.+)/;
        let yearResult = yearReg.exec(planId);
        let monthResult = monthReg.exec(planId);
        let weekResult = weekReg.exec(planId);
        let dayResult = dayReg.exec(planId);
        let currentYear = years[0];
        let currentMonth = null;
        let currentDay = null;
        let dayTimeMSecond = 24 * 3600 * 1000;
        let weekStartDay = null;
        let weekEndDay = null;
        const nowDate = new Date();
        if (yearResult) {
            currentYear = yearResult[1];
        }
        if (nowDate.getFullYear() == currentYear) {
            currentMonth = nowDate.getMonth() + 1;
            currentDay = getDateStr(nowDate);
            let weekDay = nowDate.getDay();
            weekStartDay = getDateStr(new Date(nowDate.getTime() - weekDay * dayTimeMSecond));
        } else {
            currentMonth = `1`;
            let monthFirstDay = `${currentYear}/${currentMonth}/1`;
            let monthFirstDayDate = new Date(`${currentYear}/${currentMonth}/1`);
            weekStartDay = currentDay = getDateStr(new Date(monthFirstDayDate.getTime() - monthFirstDayDate.getDay() * dayTimeMSecond));
        }
        if (monthResult) {
            currentYear = monthResult[1].split('.')[0];
            currentMonth = monthResult[1].split('.')[1];
            if (currentYear == nowDate.getFullYear() && currentMonth == nowDate.getMonth() + 1) {
                currentDay = getDateStr(nowDate);
                let weekDay = nowDate.getDay();
                weekStartDay = getDateStr(new Date(nowDate.getTime() - weekDay * dayTimeMSecond));
            } else {
                let monthFirstDay = `${currentYear}/${currentMonth}/1`;
                let monthFirstDayDate = new Date(`${currentYear}/${currentMonth}/1`);
                weekStartDay = currentDay = getDateStr(new Date(monthFirstDayDate.getTime() - monthFirstDayDate.getDay() * dayTimeMSecond));
            }
        }
        if (weekResult) {
            const weekRange = weekResult[1].split('_');
            weekStartDay = convertDateStr(weekRange[0]);
            weekEndDay = convertDateStr(weekRange[1]);
            currentYear = weekEndDay.split('/')[0];
            currentMonth = weekEndDay.split('/')[1];
            if (nowDate.getTime() > new Date(weekRange[0]).getTime() && nowDate.getTime() < new Date(weekRange[1]).getTime()) {
                currentDay = getDateStr(nowDate);
            } else {
                currentDay = weekStartDay;
            }
        }
        if (dayResult) {
            currentDay = convertDateStr(dayResult[1]);
            let tempDate = new Date(currentDay);
            let weekDay = tempDate.getDay();
            weekStartDay = getDateStr(new Date(tempDate.getTime() - weekDay * dayTimeMSecond));
            weekEndDay = getDateStr(new Date(new Date(weekStartDay).getTime() + 7 * dayTimeMSecond));
            currentYear = tempDate.getFullYear();
            currentMonth = tempDate.getMonth() + 1;
        }
        weekEndDay = getDateStr(new Date(new Date(weekStartDay).getTime() + 7 * dayTimeMSecond));
        for (let i = 0; i < years.length; i++) {
            var year = years[i];
            yearList.push({
                active: (year == currentYear && (yearResult || monthResult || weekResult || dayResult)) ? true : false,
                value: `year_${years[i]}`,
                label: `${years[i]}`
            });
            if (year == currentYear && yearResult && yearList.length > 0) {
                let nowYearIns = new Date(`${currentYear}/1/1`);
                currentChoose = {
                    type: 'year',
                    today: yearList[yearList.length - 1].value,
                    pre: `year_${new Date(nowYearIns.getTime() - 24 * 365 * 3600 * 1000).getFullYear()}`
                }
            }
        }
        for (let i = 0; i < 12; i++) {
            let month = i + 1;
            monthList.push({
                value: `month_${currentYear}.${month}`,
                label: `${month}月`,
                active: (month == currentMonth && (monthResult || weekResult || dayResult)) ? true : false
            });
            if (month == currentMonth && (monthResult) && monthList.length > 0) {
                let currentMonthIns = new Date(`${currentYear}/${currentMonth}/1`);
                let newMonthIns = new Date(currentMonthIns.getTime() - 24 * 30 * 3600 * 1000)
                currentChoose = {
                    type: 'month',
                    today: monthList[monthList.length - 1].value,
                    pre: `month_${newMonthIns.getFullYear()}.${newMonthIns.getMonth() + 1}`
                }
            }
        }
        let monthStartDayDate = new Date(`${currentYear}/${currentMonth}/1`);
        let monthStartDay = monthStartDayDate.getDay();
        let monthWeekStartDay = new Date(monthStartDayDate.getTime() - monthStartDay * dayTimeMSecond);
        let tempWeekStartDay = monthWeekStartDay;
        for (let i = 0; i < 5; i++) {
            let tempWeekEndDay = new Date(tempWeekStartDay.getTime() + 6 * dayTimeMSecond);
            let nextStartDay = new Date(tempWeekStartDay.getTime() + 7 * dayTimeMSecond);
            let weekId = `${getDateStr(tempWeekStartDay)}_${getDateStr(tempWeekEndDay)}`;
            weekList.push({
                value: `week_${getDateHref(weekId)}`,
                label: `${getDateHref(weekId)}`,
                active: (getDateStr(tempWeekStartDay) == weekStartDay && (weekResult || dayResult)) ? true : false
            });
            if (getDateStr(tempWeekStartDay) == weekStartDay && weekResult && weekList.length > 0) {
                currentChoose = {
                    type: 'week',
                    today: weekList[weekList.length - 1].value,
                    pre: 'week_' + getDateHref(`${getDateStr(new Date(tempWeekStartDay.getTime() - 7 * 24 * 3600 * 1000))}_${getDateStr(new Date(tempWeekEndDay.getTime() - 7 * 24 * 3600 * 1000))}`)
                }
            }
            tempWeekStartDay = nextStartDay;
        }
        let tempStartDay = new Date(weekStartDay);
        for (let i = 0; i < 7; i++) {
            let dayId = getDateStr(tempStartDay);
            dayList.push({
                value: `day_${getDateHref(dayId)}`,
                label: `${getDateHref(dayId)}`,
                active: (currentDay == getDateStr(tempStartDay) && dayResult) ? true : false
            });
            if (currentDay == getDateStr(tempStartDay) && dayResult && dayList.length > 0) {
                currentChoose = {
                    type: 'day',
                    today: dayList[dayList.length - 1].value,
                    pre: `day_${getDateHref(getDateStr(new Date(new Date(currentDay).getTime() - 24 * 3600 * 1000)))}`
                }
            }
            tempStartDay = new Date(tempStartDay.getTime() + dayTimeMSecond);
        }
    }
    return {
        yearList,
        monthList,
        weekList,
        dayList,
        currentChoose
    }
}
